//
//  RCUserEmailRegVC.m
//  RocketProject
//
//  Created by chen jiedan on 13-8-25.
//  Copyright (c) 2013年 Rocket. All rights reserved.
//

#import "RCUserEmailRegVC.h"
#import "CommonDefines.h"
#import "UIColor-Expanded.h"
#import "IManager+RegisterRequest.h"
#import "RCPhoneVerifyDataModel.h"
#import "AppDelegate.h"
#import "GloableEngineService.h"
#import "RCLoginController.h"
#import "RCTagsController.h"


@interface RCUserEmailRegVC ()<IManagerDelegate>{
    UITextField *_emailField;
    UITextField *_nickNameField;
    UITextField *_passwordField;
//    UITextField *_verifyCodeFiled;
    UIButton    *_sendBtn;
    int64_t     _reqID;
    BOOL        isCheck;
    
}

@end

@implementation RCUserEmailRegVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.navigationController.navigationBarHidden = YES;
    
    for (int i = 0; i < 3; i ++) {
        
        UIImageView *bg1 = [[[UIImageView alloc] initWithFrame:CGRectMake(14, 12+60*i, 320-14*2, 47)] autorelease];
        UIImage *img1 = [UIImage imageNamed:@"form_bg1"];
        bg1.image = img1;
        [self.view addSubview:bg1];
        
        UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 15, 70, 16)];
        [bg1 addSubview:leftLabel];
        [leftLabel setFont:[UIFont fontWithName:kFontNameLightHeiti size:16]];
        leftLabel.textColor = [UIColor colorWithHex:0xff979797];
       
        
        
        UITextField *filed = [[UITextField alloc] initWithFrame:CGRectMake(100, 25+60*i, 200, 20)];
        [self.view addSubview:filed];
        [filed setBackgroundColor:[UIColor clearColor]];
        filed.clearButtonMode = UITextFieldViewModeWhileEditing;
        filed.font = [UIFont systemFontOfSize:16];
        //    _userNameLabel.tag = kTagUserName;
        filed.returnKeyType = UIReturnKeyDone;
        //    _userNameLabel.delegate = self;
        
        if (i == 0) {
            
            leftLabel.text = @"电子邮箱";
            _emailField = filed;
            _emailField.placeholder = @"email";

        }else if ( i == 1){
            leftLabel.text = @"昵称";

            _nickNameField = filed;
             _nickNameField.placeholder = @"中文、英文或数字";

        }else if ( i == 2){
            leftLabel.text = @"密码";
            _passwordField = filed;
            _passwordField.secureTextEntry = YES;
        }
        [leftLabel release];
        [filed release];
        
    }
    

    
    
    
    UIButton *checkBox = [[UIButton alloc] initWithFrame:CGRectMake(14, 185, 20, 20)];
    [self.view addSubview:checkBox];
    [checkBox addTarget:self action:@selector(checkLawClick:) forControlEvents:UIControlEventTouchUpInside];
    [checkBox setBackgroundImage:[UIImage imageNamed:@"icon_law_uncheck"] forState:UIControlStateNormal];
    [checkBox release];
    
    UILabel *lawLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 190, 200, 15)];
    [lawLabel setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:lawLabel];
    lawLabel.font = [UIFont systemFontOfSize:10];
    [lawLabel release];
    lawLabel.text = @"我已经看过并同意如意行《用户使用协议》";
    
    
    _sendBtn = [[UIButton alloc] initWithFrame:CGRectMake(14, 210, 320-28, 40)];
    [_sendBtn setTitle:@"提交" forState:UIControlStateNormal];
    [self.view addSubview:_sendBtn];
    UIImage *loginIMG = [UIImage imageNamed:@"login_btn"];
    [_sendBtn setBackgroundImage:loginIMG forState:UIControlStateNormal];
    //        [loginBtn setBackgroundColor:[UIColor blackColor]];
    [_sendBtn addTarget:self action:@selector(regPress:) forControlEvents:UIControlEventTouchUpInside];
    [_sendBtn release];
    
    
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





-(void)  checkLawClick:(id)sender
{
    isCheck = !isCheck;
    UIButton *t = (UIButton *)sender;
    if (isCheck) {
        [t setBackgroundImage:[UIImage imageNamed:@"icon_law_unchecked"] forState:UIControlStateNormal];
    }else{
        [t setBackgroundImage:[UIImage imageNamed:@"icon_law_uncheck"] forState:UIControlStateNormal];
    }
    //    [t setBackgroundColor:[UIColor blueColor]];
}


//点击提交按钮
- (void) regPress:(id)sender
{
    NSString *email = _emailField.text;
    NSString *uName = _nickNameField.text;
    NSString *pass  = _passwordField.text;
    
    _reqID = [[IManager sharedInstance] requestEmail:email withUserName:uName withPassword:pass withPhoneType:NO withDelegate:self];
}


- (void) updateViewForError:(NSError *)errorInfo withRequestId:(int64_t)requestId
{
    [RCAppDelegate alertWithCustomizeString:@"注册失败"];
    
}

- (void) updateViewForSuccess:(IDataModel *)dataModel withRequestId:(int64_t)requestId
{
    RCEmailRegDataModel *model = (RCEmailRegDataModel *)dataModel;
    if (model.errorNO == RCErrorTypeNoError) {
        //注册成功
        RCLoginDataModel *loginInfo = [[RCLoginDataModel alloc] init];
        loginInfo.nickName = _nickNameField.text;
        loginInfo.bduss    = model.bduss;
        [[GloableEngineService sharedInstance] saveUserLoginInfo:loginInfo];
        [loginInfo release];
        RCLoginController *tabVC = (RCLoginController *)[[[[self.navigationController parentViewController] navigationController] viewControllers] objectAtIndex:0];
        [tabVC loginSuccess];
        [[[self.navigationController parentViewController] navigationController] popToRootViewControllerAnimated:YES];
        
    }else{
        [RCAppDelegate alertWithCustomizeString:model.errorMsg];
    }
    

    
}
@end
